package com.ticket.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ticket.common.core.domain.entity.SysDept;
import com.ticket.common.exception.base.BaseException;
import com.ticket.common.utils.Const;
import com.ticket.common.utils.DateUtils;
import com.ticket.system.domain.YanchuPerform;
import com.ticket.system.mapper.SysDeptMapper;
import com.ticket.system.mapper.YanchuPerformMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ticket.system.mapper.TYanchuPostersMapper;
import com.ticket.system.domain.TYanchuPosters;
import com.ticket.system.service.ITYanchuPostersService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 海报管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-27
 */
@Service
public class TYanchuPostersServiceImpl implements ITYanchuPostersService 
{
    @Autowired
    private TYanchuPostersMapper tYanchuPostersMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private YanchuPerformMapper yanchuPerformMapper;

    /**
     * 查询海报管理
     * 
     * @param id 海报管理主键
     * @return 海报管理
     */
    @Override
    public TYanchuPosters selectTYanchuPostersById(Long id)
    {
        TYanchuPosters tYanchuPosters = tYanchuPostersMapper.selectTYanchuPostersById(id);
        if (null != tYanchuPosters){
            if (Const.TWO.equals(tYanchuPosters.getActType())){
                //处理剧场名称
                SysDept sysDept = sysDeptMapper.selectDeptById(tYanchuPosters.getOperaId());
                if (null != sysDept){
                    tYanchuPosters.setOperaName(sysDept.getDeptName());
                }
                //演出名称处理
                YanchuPerform perform = yanchuPerformMapper.selectYanchuPerformById(tYanchuPosters.getMovieId());
                if (null != perform){
                    tYanchuPosters.setMovieName(perform.getPerformName());
                }
            }
        }
        return tYanchuPosters;
    }

    /**
     * 查询海报管理列表
     * 
     * @param tYanchuPosters 海报管理
     * @return 海报管理
     */
    @Override
    public List<TYanchuPosters> selectTYanchuPostersList(TYanchuPosters tYanchuPosters)
    {
        List<TYanchuPosters> posters = tYanchuPostersMapper.selectTYanchuPostersList(tYanchuPosters);
        if (CollectionUtils.isNotEmpty(posters)){
            List<Long> operaIds = posters.stream().filter(p -> Const.TWO.equals(p.getActType())).map(TYanchuPosters::getOperaId).collect(Collectors.toList());
            List<Long> movieIds = posters.stream().filter(p -> Const.TWO.equals(p.getActType())).map(TYanchuPosters::getMovieId).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(operaIds)) {
                //剧院名称处理
                List<SysDept> sysDepts = sysDeptMapper.selectDeptByIds(operaIds);
                if (CollectionUtils.isNotEmpty(sysDepts)){
                    posters.forEach(p ->{
                        sysDepts.forEach(s -> {
                            if (null != p.getOperaId() && p.getOperaId().equals(s.getDeptId())){
                                p.setOperaName(s.getDeptName());
                            }
                        });
                    });
                }
            }
            //演出名称处理
            if (CollectionUtils.isNotEmpty(movieIds)){
                List<YanchuPerform> performs = yanchuPerformMapper.selectPerformByIds(movieIds);
                if (CollectionUtils.isNotEmpty(performs)){
                    posters.forEach(p -> {
                        performs.forEach(f ->{
                            if (null != p.getMovieId() && p.getMovieId().equals(f.getId())){
                                p.setMovieName(f.getPerformName());
                            }
                        });
                    });
                }
            }
        }
        return posters;
    }

    /**
     * 新增海报管理
     * 
     * @param tYanchuPosters 海报管理
     * @return 结果
     */
    @Override
    public int insertTYanchuPosters(TYanchuPosters tYanchuPosters)
    {
        //获取最大排序值
        Integer maxOrdeNum = tYanchuPostersMapper.getMaxOrdeNum();
        if (null == maxOrdeNum){
            maxOrdeNum = 1;
        }else {
            maxOrdeNum ++;
        }

        tYanchuPosters.setCreateTime(DateUtils.getNowDate());
        tYanchuPosters.setStatus(0);
        tYanchuPosters.setOrderNum(maxOrdeNum);
        return tYanchuPostersMapper.insertTYanchuPosters(tYanchuPosters);
    }

    /**
     * 修改海报管理
     * 
     * @param tYanchuPosters 海报管理
     * @return 结果
     */
    @Override
    public int updateTYanchuPosters(TYanchuPosters tYanchuPosters)
    {
        return tYanchuPostersMapper.updateTYanchuPosters(tYanchuPosters);
    }

    /**
     * 批量删除海报管理
     * 
     * @param ids 需要删除的海报管理主键
     * @return 结果
     */
    @Override
    public int deleteTYanchuPostersByIds(List<Long> ids)
    {
        return tYanchuPostersMapper.deleteTYanchuPostersByIds(ids);
    }

    /**
     * 删除海报管理信息
     * 
     * @param id 海报管理主键
     * @return 结果
     */
    @Override
    public int deleteTYanchuPostersById(Long id)
    {
        return tYanchuPostersMapper.deleteTYanchuPostersById(id);
    }

    /**
     * 上移下移
     *
     * @param id
     * @param type
     * @return
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int upOrDown(Long id, Integer type,Integer orderNum) {
        Integer maxOrdeNum = tYanchuPostersMapper.getMaxOrdeNum();
        if (Const.ONE.equals(type) && orderNum.equals(maxOrdeNum)){
            throw new BaseException("已经是第一个,无法上移。");
        }
        Integer minOrderNum = tYanchuPostersMapper.getMinOrderNum();
        if (Const.TWO.equals(type) && orderNum.equals(minOrderNum)){
            throw new BaseException("已经是最后一个,无法下移。");
        }
        List<TYanchuPosters> tYanchuPosters = tYanchuPostersMapper.selectTYanchuPostersList(new TYanchuPosters());
        if (CollectionUtils.isNotEmpty(tYanchuPosters)){
            for (int i = 0; i < tYanchuPosters.size(); i++) {
                if (tYanchuPosters.get(i).getId().equals(id)){
                    TYanchuPosters posters = tYanchuPosters.get(i);
                    TYanchuPosters postersUp = new TYanchuPosters();
                    if (Const.ONE.equals(type)) {
                        //上移
                        postersUp = tYanchuPosters.get(i - 1);
                    }else {
                        //下移
                        postersUp = tYanchuPosters.get(i + 1);
                    }
                    Integer num = postersUp.getOrderNum();
                    postersUp.setOrderNum(posters.getOrderNum());
                    posters.setOrderNum(num);
                    tYanchuPostersMapper.updateTYanchuPosters(postersUp);
                    tYanchuPostersMapper.updateTYanchuPosters(posters);
                    return 1;
                }
            }
        }
        return -1;
    }

    /**
     * 海报状态编辑
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int statueOpenOrClose(Long id, Integer status) {
        Assert.notNull(id,"缺少id");
        Assert.notNull(status,"缺少状态");
        TYanchuPosters posters = new TYanchuPosters();
        posters.setStatus(status);
        if (Const.ONE.equals(status)){
            //判断海报开启状态是否超过规定数量
            List<TYanchuPosters> tYanchuPosters = tYanchuPostersMapper.selectTYanchuPostersList(posters);
            if (CollectionUtils.isNotEmpty(tYanchuPosters) && tYanchuPosters.size() >= 8){
                throw new BaseException("开启状态已达到上限");
            }
        }
        posters.setId(id);
        return tYanchuPostersMapper.updateTYanchuPosters(posters);
    }

    /**
     * 名称是否存在
     *
     * @param postersName
     * @return
     */
    @Override
    public boolean hasPosterName(String postersName) {
        return tYanchuPostersMapper.hasPosterName(postersName) > 0;
    }

}
